﻿<?php include("conn.php");
 mysql_query('SET NAMES UTF8'); 

	  

class MyPrizeBean{
	
	public $name;
    public $shequ;
	public $cityid;
	public $dic;
    public $lon;
    public $lat;

	
	function __construct($name,$shequ,$cityid,$dic,$lon,$lat)
	{
		
		$this->name      = $name;
        $this->shequ      = $shequ;
		$this->cityid      = $cityid;
		$this->dic      = $dic;
        $this->lon      = $lon;
        $this->lat      = $lat;
	
	}
}


$prize = array();


//最近函数
define(EARTH_RADIUS, 6371);//地球半径，平均半径为6371km
 /**
 *计算某个经纬度的周围某段距离的正方形的四个点
 *
 *@param lng float 经度
 *@param lat float 纬度
 *@param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
 *@return array 正方形的四个点的经纬度坐标
 */
 function returnSquarePoint($lng,$lat,$distance = 50){
 
    $dlng =  50 * asin(sin($distance / (50 * EARTH_RADIUS)) / cos(deg2rad($lat)));
    $dlng = rad2deg($dlng);
     
    $dlat = $distance/EARTH_RADIUS;
    $dlat = rad2deg($dlat);
     
    return array(
                'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
                'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
                'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
                'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
                );
 }
//使用此函数计算得到结果后，带入sql查询。
$squares = returnSquarePoint($_REQUEST[lon],$_REQUEST[lat]);
//最近结束
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {//计算距离
    $theta = $longitude1 - $longitude2;
    $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
    $miles = acos($miles);
    $miles = rad2deg($miles);
    $miles = $miles * 60 * 1.1515;
    $kilometers = $miles * 1.609344;
    return compact('kilometers'); 

}//计算距离 end
$query = mysql_query("select * from `city` where latitude<>0 and latitude>{$squares['right-bottom']['lat']} and latitude<{$squares['left-top']['lat']} and longitude>{$squares['left-top']['lng']} and longitude<{$squares['right-bottom']['lng']} ");
$rowf=mysql_fetch_array($query);
if (!$rowf) {
    $query = mysql_query("select * from `city` limit 1 ");
    $rowf=mysql_fetch_array($query);
}

$longitude1=$_REQUEST['lon'];$latitude1=$_REQUEST['lat'];$longitude2=$rowf['longitude'];$latitude2=$rowf['latitude'];
	   

$distance = getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2);

foreach ($distance as $unit => $value) {
    $dic= number_format($value,1);
}

	$prize[] = new MyPrizeBean($rowf['name'],$rowf['city'],$rowf['id'],$dic,$longitude2,$latitude2);
	


// foreach ($prize as $key => $value) {
// 	$value=object_array($value);
// }
$arr= array();
foreach ($prize as $key => $value) {
	$arr[$key]=objectToArray($value);
}

function objectToArray($obj){
    $arr = is_object($obj) ? get_object_vars($obj) : $obj;
    if(is_array($arr)){
        return array_map(__FUNCTION__, $arr);
    }else{
        return $arr;
    }
}


foreach ($arr as $k => $v) {
	$d[$k]=$v[dic];
}
$mindic=min($d);
$minprize = array();

foreach ($arr as $k => $v) {
	if ($v[dic]==$mindic) {
		$minprize['data'] = $v;
        $minprize['code'] = 200;
        $minprize['msg'] = '获取成功';
        echo json_encode($minprize);

        exit;
	}
}

if(count($arr) > 0){
    $minprize['code'] = -100;
    $minprize['msg'] = '获取失败';
}else{
    $minprize['code'] = -100;
    $minprize['msg'] = '没有数据';
}
echo json_encode($minprize);


?>